package com.linx.open.utils;

import com.linx.common.core.utils.HttpClientUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName: HttpUtils
 * @Description:
 * @Author: jxw
 * @Date: 2024-12-31 15
 */
public class TestUtils {


    public static void main(String[] args) {
        try {
            String username = "admin";
            String password = "admin";
            String encryptedPassword = EncryptionUtils.encryption(password);
            System.out.println("加密后的密码: " + encryptedPassword);

            String apiResponse = callAPI(username, encryptedPassword);
            System.out.println("接口返回内容: " + apiResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送POST请求到指定接口
     *
     * @param username 用户名
     * @param encryptedPassword 加密后的密码
     * @return 接口返回的响应内容
     * @throws IOException 如果HTTP请求出现IO相关问题抛出异常
     */
    public static String callAPI(String username, String encryptedPassword) throws IOException {
        // 对密码进行URL编码
        String encodedPassword = URLEncoder.encode(encryptedPassword, StandardCharsets.UTF_8);
        System.out.println("URLEncoder后的密码: " + encodedPassword);
        String url = "http://221.213.61.70:38880/v1/rbac/token?username=" + username + "&password=" + encodedPassword;
        return HttpClientUtils.sendPost(url, null);
    }

}
